﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace Cqrs.Infrastructure
{
    public class StateMachineRecord
    {
        public string Name { get; set; }
        public string AssemblyType { get; set; }
        public string AssemblyLocation { get; set; }
        public string StateMachineXml { get; set; }
        public string StateMachineDesigner { get; set; }

        //public static StateMachineRecord GetStateMachine(Type domainObject)
        //{
        //    StringBuilder sql = new StringBuilder();
        //    List<SqlParameter> parameters = new List<SqlParameter>();
        //    parameters.Add(new SqlParameter("@assemblyType", SqlDbType.NVarChar));
        //    parameters[0].Value = domainObject.AssemblyQualifiedName;

        //    DataTable dt = Database.Utils.GetDataTable(
        //        "SELECT Name, AssemblyType, AssemblyLocation, StateMachineXml, StateMachineDesigner FROM StateMachines WHERE AssemblyType=@assemblyType",
        //        parameters);


        //    if (dt.Rows.Count == 0)
        //        return null;
        //    else
        //        return new StateMachineRecord()
        //            {
        //                Name = dt.Rows[0]["Name"].ToString(),
        //                AssemblyType = dt.Rows[0]["AssemblyType"].ToString(),
        //                AssemblyLocation = dt.Rows[0]["AssemblyLocation"].ToString(),
        //                StateMachineXml = dt.Rows[0]["StateMachineXml"].ToString(),
        //                StateMachineDesigner = dt.Rows[0]["StateMachineDesigner"].ToString()
        //            };

        //}

        public static StateMachineRecord GetStateMachine(Type domainObject, 
            XElement stateMachineXml, XElement stateMachineDesigner)
        {
            return new StateMachineRecord()
            {
                Name = domainObject.FullName,
                AssemblyType = domainObject.AssemblyQualifiedName,
                AssemblyLocation = domainObject.Assembly.Location,
                StateMachineXml = stateMachineXml.ToString(),
                StateMachineDesigner = stateMachineDesigner.ToString()
            };
        }

        //public static IList<StateMachineRecord> GetAllStateMachines()
        //{
        //    StringBuilder sql = new StringBuilder();
        //    DataTable dt = Database.Utils.GetDataTable("SELECT Name, AssemblyType, AssemblyLocation, StateMachineXml, StateMachineDesigner FROM StateMachines ORDER BY Name");
        //    List<StateMachineRecord> machines = new List<StateMachineRecord>();

        //    foreach (DataRow row in dt.Rows)
        //    {
        //        machines.Add(
        //            new StateMachineRecord()
        //            {
        //                Name = row["Name"].ToString(),
        //                AssemblyType = row["AssemblyType"].ToString(),
        //                AssemblyLocation = row["AssemblyLocation"].ToString(),
        //                StateMachineXml = row["StateMachineXml"].ToString(),
        //                StateMachineDesigner = row["StateMachineDesigner"].ToString()
        //            }
        //       );
        //    }
        //    return machines;
        //}

        //public static void InsertStateMachine(StateMachineRecord machine)
        //{
        //    using (SqlConnection db = Database.Utils.Connection())
        //    {
        //        db.Open();
        //        StringBuilder sql = new StringBuilder();
        //        sql.AppendLine("INSERT INTO StateMachines");
        //        sql.AppendLine("(Name, AssemblyType, AssemblyLocation, StateMachineXml, StateMachineDesigner) VALUES");
        //        sql.AppendLine("(@Name, @assemblyType, @assemblyLocation, @StateMachineXml, @StateMachineDesigner)");
        //        SqlCommand comm = new SqlCommand(sql.ToString(), db);
        //        comm.Parameters.Add("@Name", SqlDbType.NVarChar).Value = machine.Name;
        //        comm.Parameters.Add("@assemblyType", SqlDbType.NVarChar).Value = machine.AssemblyType;
        //        comm.Parameters.Add("@assemblyLocation", SqlDbType.NVarChar).Value = machine.AssemblyLocation;
        //        comm.Parameters.Add("@StateMachineXml", SqlDbType.Xml).Value = machine.StateMachineXml;
        //        comm.Parameters.Add("@StateMachineDesigner", SqlDbType.Xml).Value = machine.StateMachineDesigner;
        //        comm.ExecuteNonQuery();
        //        db.Close();
        //    }
        //}

        //public static void UpdateStateMachine(StateMachineRecord machine)
        //{
        //    using (SqlConnection db = Database.Utils.Connection())
        //    {
        //        db.Open();
        //        StringBuilder sql = new StringBuilder();
        //        sql.AppendLine("UPDATE StateMachines");
        //        sql.AppendLine("SET Name = @Name, AssemblyType = @assemblyType, AssemblyLocation = @assemblyLocation, StateMachineXml = @StateMachineXml, StateMachineDesigner = @StateMachineDesigner");
        //        sql.AppendLine("WHERE Name = @Name");
        //        SqlCommand comm = new SqlCommand(sql.ToString(), db);
        //        comm.Parameters.Add("@Name", SqlDbType.NVarChar).Value = machine.Name;
        //        comm.Parameters.Add("@assemblyType", SqlDbType.NVarChar).Value = machine.AssemblyType;
        //        comm.Parameters.Add("@assemblyLocation", SqlDbType.NVarChar).Value = machine.AssemblyLocation;
        //        comm.Parameters.Add("@StateMachineXml", SqlDbType.Xml).Value = machine.StateMachineXml;
        //        comm.Parameters.Add("@StateMachineDesigner", SqlDbType.Xml).Value = machine.StateMachineDesigner;
        //        comm.ExecuteNonQuery();
        //        db.Close();
        //    }
        //}

        //public static void DeleteStateMachine(string name)
        //{
        //    using (SqlConnection db = Database.Utils.Connection())
        //    {
        //        db.Open();
        //        StringBuilder sql = new StringBuilder();
        //        sql.AppendLine("DELETE FROM StateMachines");
        //        sql.AppendLine("WHERE Name = @Name");
        //        SqlCommand comm = new SqlCommand(sql.ToString(), db);
        //        comm.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
        //        comm.ExecuteNonQuery();
        //        db.Close();
        //    }
        //}
    }
}
